package com.ruoyi.pollution.service.impl;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pollution.domain.BusPsCodOutputpollutant;
import com.ruoyi.pollution.domain.BusPsCodPollutant;
import com.ruoyi.pollution.mapper.BusPsCodOutputpollutantMapper;
import com.ruoyi.pollution.mapper.BusPsCodPollutantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pollution.mapper.BusPsMidWaterhourdataMapper;
import com.ruoyi.pollution.domain.BusPsMidWaterhourdata;
import com.ruoyi.pollution.service.IBusPsMidWaterhourdataService;
import org.springframework.util.CollectionUtils;

/**
 * 小时污染物监测数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-18
 */
@Service
public class BusPsMidWaterhourdataServiceImpl implements IBusPsMidWaterhourdataService 
{
    @Autowired
    private BusPsMidWaterhourdataMapper busPsMidWaterhourdataMapper;
    @Autowired
    private BusPsCodPollutantMapper busPsCodPollutantMapper;
    @Autowired
    private BusPsCodOutputpollutantMapper busPsCodOutputpollutantMapper;

    /**
     * 查询小时污染物监测数据
     * 
     * @param entcode 小时污染物监测数据主键
     * @return 小时污染物监测数据
     */
    @Override
    public BusPsMidWaterhourdata selectBusPsMidWaterhourdataByEntcode(String entcode)
    {
        return busPsMidWaterhourdataMapper.selectBusPsMidWaterhourdataByEntcode(entcode);
    }

    /**
     * 查询小时污染物监测数据列表
     * 
     * @param busPsMidWaterhourdata 小时污染物监测数据
     * @return 小时污染物监测数据
     */
    @Override
    public List<BusPsMidWaterhourdata> selectBusPsMidWaterhourdataList(BusPsMidWaterhourdata busPsMidWaterhourdata)
    {
        return busPsMidWaterhourdataMapper.selectBusPsMidWaterhourdataList(busPsMidWaterhourdata);
    }

    @Override
    public Map onlineMonitorList(String id, String pid, String startTime, String endTime) {
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";

        Map resMap = new HashMap();

        List<Map> tableOp = new ArrayList<>();
        List<Map> tableData = new ArrayList<>();
        //查询所有污染物
        List<BusPsCodPollutant> busPsCodPollutantList = busPsCodPollutantMapper.selectBusPsCodPollutantList(new BusPsCodPollutant());
        Map<String,BusPsCodPollutant> busPsCodPollutantMap = new HashMap<>();
        if(!CollectionUtils.isEmpty(busPsCodPollutantList)){
            for (BusPsCodPollutant busPsCodPollutant : busPsCodPollutantList) {
                busPsCodPollutantMap.put(busPsCodPollutant.getPollutantcode(),busPsCodPollutant);
            }
        }

        //查询关联的污染因子
        BusPsCodOutputpollutant codOutputpollutantParam = new BusPsCodOutputpollutant();
        codOutputpollutantParam.setOutportcode(id);
        List<BusPsCodOutputpollutant> codOutputpollutantList = busPsCodOutputpollutantMapper.selectBusPsCodOutputpollutantList(codOutputpollutantParam);

        int columnIndex = 1;
        Map timeopTitle = new HashMap();
        timeopTitle.put("prop","column"+columnIndex);
        timeopTitle.put("label","监测时间");
        tableOp.add(timeopTitle);
        if(!CollectionUtils.isEmpty(codOutputpollutantList) && !CollectionUtils.isEmpty(busPsCodPollutantList)){
            for (BusPsCodOutputpollutant codOutputpollutant : codOutputpollutantList) {
                if(busPsCodPollutantMap.containsKey(codOutputpollutant.getPollutantcode())){
                    columnIndex++;
                    Map opTitle = new HashMap();
                    opTitle.put("prop","column"+columnIndex);
                    BusPsCodPollutant busPsCodPollutant = busPsCodPollutantMap.get(codOutputpollutant.getPollutantcode());
                    if(StringUtils.isNotEmpty(busPsCodPollutant.getUnit())){
                        opTitle.put("label",busPsCodPollutant.getPollutantname()+"("+busPsCodPollutant.getUnit()+")");
                    }else{
                        opTitle.put("label",busPsCodPollutant.getPollutantname());
                    }
                    tableOp.add(opTitle);
                }
            }

            BusPsMidWaterhourdata dataParam = new BusPsMidWaterhourdata();
            dataParam.setOutportcode(id);
            dataParam.setStartTime(startTime);
            dataParam.setEndTime(endTime);
            List<BusPsMidWaterhourdata> dataList = selectBusPsMidWaterhourdataList(dataParam);
            if(!CollectionUtils.isEmpty(dataList)){
                //计算所有时间
                Set<String> timeSet = new LinkedHashSet<>();
                Map<String,BusPsMidWaterhourdata> hourdataMap = new HashMap<>();
                for (BusPsMidWaterhourdata busPsMidWaterhourdata : dataList) {
                    String hour = DateUtils.parseDateToStr("yyy-MM-dd HH",busPsMidWaterhourdata.getMonitortime());
                    timeSet.add(hour);
                    hourdataMap.put(hour+"_"+busPsMidWaterhourdata.getPollutantcode(),busPsMidWaterhourdata);
                }

                for (String time : timeSet) {
                    Map dataMap = new HashMap();
                    int column = 1;
                    dataMap.put("column"+column,time);
                    for (BusPsCodOutputpollutant codOutputpollutant : codOutputpollutantList) {
                        column++;
                        if(hourdataMap.containsKey(time+"_"+codOutputpollutant.getPollutantcode())){
                            BusPsMidWaterhourdata hourdata = hourdataMap.get(time+"_"+codOutputpollutant.getPollutantcode());
                            dataMap.put("column"+column,hourdata.getAvgstrength());
                        }else{
                            dataMap.put("column"+column,"");
                        }
                    }
                    tableData.add(dataMap);
                }
            }
        }
        resMap.put("tableOp",tableOp);
        resMap.put("tableData",tableData);
        return resMap;
    }

    /**
     * 新增小时污染物监测数据
     * 
     * @param busPsMidWaterhourdata 小时污染物监测数据
     * @return 结果
     */
    @Override
    public int insertBusPsMidWaterhourdata(BusPsMidWaterhourdata busPsMidWaterhourdata)
    {
        return busPsMidWaterhourdataMapper.insertBusPsMidWaterhourdata(busPsMidWaterhourdata);
    }

    /**
     * 修改小时污染物监测数据
     * 
     * @param busPsMidWaterhourdata 小时污染物监测数据
     * @return 结果
     */
    @Override
    public int updateBusPsMidWaterhourdata(BusPsMidWaterhourdata busPsMidWaterhourdata)
    {
        return busPsMidWaterhourdataMapper.updateBusPsMidWaterhourdata(busPsMidWaterhourdata);
    }

    /**
     * 批量删除小时污染物监测数据
     * 
     * @param entcodes 需要删除的小时污染物监测数据主键
     * @return 结果
     */
    @Override
    public int deleteBusPsMidWaterhourdataByEntcodes(String[] entcodes)
    {
        return busPsMidWaterhourdataMapper.deleteBusPsMidWaterhourdataByEntcodes(entcodes);
    }

    /**
     * 删除小时污染物监测数据信息
     * 
     * @param entcode 小时污染物监测数据主键
     * @return 结果
     */
    @Override
    public int deleteBusPsMidWaterhourdataByEntcode(String entcode)
    {
        return busPsMidWaterhourdataMapper.deleteBusPsMidWaterhourdataByEntcode(entcode);
    }


}
